package cn.dlc.com.controller;

import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.dlc.com.config.ServerProvider;

/**
 * 消费者方的真实服务
 * @author Administrator
 *
 */
@RestController
@RequestMapping("consumer")
public class ConsumerController {

	@Autowired
	private ServerProvider sp;
	
	@Value("${zookeeper.nodePath}")
	private String nodePath;
	
	@Value("${zookeeper.serverName}")
	private String serverName;
	
	@GetMapping
	public String consumer() {
		
		// 获取所有的地址
		List<String> serversList = sp.getServersList(nodePath, serverName);
		for (String url : serversList) {
			System.out.println(url);
		}
		// 负载均衡，没错，就这么点东西
		int index = new Random().nextInt(serversList.size());
		
		return serversList.get(index);
	}
}
